package com.fsnmt.maizhuanbao.utils;

import android.graphics.Bitmap;
import android.text.TextUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileUtils {

	/**
	 * 判断文件或文件夹是否存在
	 *
	 * @param fileName
	 *            待检查的文件名
	 * @return 无
	 */
	public static boolean isExist(String fileName) {
		File f = new File(fileName);
		return f.exists();
	}

	/**
	 * 检查一个文件创建时间是否超过指定时间
	 *
	 * @param fileName
	 *            待检查的文件名
	 * @param time
	 *            时间，单位为秒
	 * @return 超时返回true,否则返回false
	 */
	public static boolean isOutDate(String fileName, long time) {
		File f = new File(fileName);
		return System.currentTimeMillis() - f.lastModified() > (time * 1000);
	}

	/**
	 * 刪除文件。
	 * @param fileName 文件名。
	 */
	public static void deleteFile(String fileName) {
		File f = new File(fileName) ;
		if (f.exists()) {
			f.delete() ;
		}
	}

	/**
	 * 删除文件或文件夹（保留文件夹）
	 *
	 * @param file
	 *            待检查的文件名
	 * @return 无
	 */
	public static void deleteAllFiles(File file) {
		if (file.exists()) {
			if (file.isDirectory()) {
				File[] files = file.listFiles();
				for (File f : files) {
					deleteAllFiles(f);
				}
			} else if (file.isFile()) {
				file.delete();
			}
		}
	}

	/**
	 * 删除文件或文件夹（不保留文件夹）
	 */
	public static void deleteAllFilesAndDir(File file) {
		if (file.exists()) {
			if (file.isDirectory()) {
				File[] files = file.listFiles();
				if(null==files || 0==files.length ){
					file.delete();
					return;
				}
				for (File f : files) {
					deleteAllFilesAndDir(f);
				}
				file.delete();
			} else if (file.isFile()) {
				file.delete();
			}
		}
	}

	/**
	 * 删除文件或文件夹（保留文件夹）
	 *
	 * @param fileName
	 *            待检查的文件名
	 * @return 无
	 */
	public static void deleteAllFiles(String fileName) {
		File f = new File(fileName);
		deleteAllFiles(f);
	}

	/**
	 * 删除文件和文件夹（包括文件夹）
	 *
	 * @param fileName
	 *            待检查的文件名
	 * @return 无
	 */
	public static void deleteAllFilesAndDir(String fileName) {
		File f = new File(fileName);
		deleteAllFilesAndDir(f);
	}

	/**
	 * 为某个文件创建所需的文件目录
	 * @param fileName 文件路径（可以是完整的文件路径，而不仅仅是目录）
	 */
	public static void makeDirs(final String fileName) {
		int indexDivider = fileName.lastIndexOf('/') ;
		if (indexDivider >= -1) {
			String sFolder = fileName.substring(0, indexDivider) ;
			File f = new File(sFolder) ;
			if (!f.exists()){
				f.mkdirs() ;
			}
		}
	}

	/**
	 * 重命名文件
	 * @param f
	 * @param newName
	 */
	public static void renameFile(File f, String newName) {
		File newFile = new File(newName) ;
		f.renameTo(newFile) ;
	}

	/**
	 * 重命名文件
	 * @param oldName
	 * @param newName
	 */
	public static void renameFile(String oldName, String newName) {
		File oldFile = new File(oldName) ;
		renameFile(oldFile, newName) ;
	}

	/**
	 * 从文件中读取字符串。
	 * @param f
	 * @return
	 */
	public static String readFileString(File f) {
		StringBuffer sb = new StringBuffer();
		int bufferLen = 1024 * 4;
		try {
			char[] buffer = new char[bufferLen];
			FileReader reader = new FileReader(f);
			int len = 0;
			while ((len = reader.read(buffer, 0, bufferLen)) != -1) {
				sb.append(buffer, 0, len);
			}
			reader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		return new String(sb) ;
	}

	/**
	 * 从输入流中读取数据。
	 * @param is
	 * @return
	 */
	public static String readInputStreamString(InputStream is) {
		StringBuffer sb = new StringBuffer();
		byte[] buffer = new byte[1024 * 4];
		String sBuffer = null;
		int bufferLen = 0;
		try {
			while ((bufferLen = is.read(buffer, 0, buffer.length)) != -1) {
				sBuffer = new String(buffer, 0, bufferLen);
				sb.append(sBuffer);
			}
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		return new String(sb) ;
	}

	public static void saveFile(InputStream in ,File dest) {
		byte[] buffer = new byte[1024 * 4];
		FileOutputStream out = null;
		int len = 0 ;
		try {
			out = new FileOutputStream(dest);

			while ((len = in.read(buffer,0,buffer.length)) != -1) {
				out.write(buffer,0,len);
			}
			out.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(null!=in)
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			if(null!= out)
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}


	public static void copyfile(File src, File dst)  {
		InputStream in = null;
		OutputStream out = null;
		try{
			in = new FileInputStream(src);
			out = new FileOutputStream(dst);
			byte[] buf = new byte[1024];
			int len;
			while ((len = in.read(buf)) > 0) {
				out.write(buf, 0, len);
			}
			in.close();
			out.close();
		}catch(Exception e){
			e.printStackTrace();
		}finally {
			if (null != in) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != out) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void copyfile(String srcfile, String dstfile)  {
		File src = new File(srcfile);
		File dst = new File(dstfile);
		copyfile(src, dst);
	}

	public static boolean saveFile(String srcfile,String s){
		File file = new File(srcfile);
		return  saveFile(file,s);
	}

	public static boolean saveFile(File srcfile,String s){
		if(!srcfile.exists()){
			try {
				srcfile.createNewFile();
			} catch (IOException e) {
				return  false;
			}
		}
		if(TextUtils.isEmpty(s)){
			return false;
		}
		try {
			FileOutputStream out = new FileOutputStream(srcfile, false);
			out.write(s.getBytes());
			out.flush();
			out.close();
		} catch (FileNotFoundException e) {
			return  false;
		} catch (IOException e) {
			return  false;
		}
		return true;
	}

	public static void saveFile(OutputStream out, String des) {
		if(TextUtils.isEmpty(des)){
			return;
		}
		try {
			out.write(des.getBytes());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void saveBitMap(Bitmap bmap,File file){
		FileOutputStream foutput = null;
		try {
			foutput = new FileOutputStream(file);
			bmap.compress(Bitmap.CompressFormat.PNG, 100, foutput);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}finally{
			if(null != foutput){
				try {
					foutput.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void saveBitMap(Bitmap bmap,String path){
		FileOutputStream foutput = null;
		try {
			foutput = new FileOutputStream(path);
			bmap.compress(Bitmap.CompressFormat.PNG, 100, foutput);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}finally{
			if(null != foutput){
				try {
					foutput.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
